package com.service.buyer;

import com.dao.buyer.BuyerLoginDao;
import com.po.Buyerinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
@Service("buyerService")
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private BuyerLoginDao buyerDao;

    @Override
    public String login(Buyerinfo buyer, Model model, HttpSession session) {
        if (buyerDao.login(buyer) != null && buyerDao.login(buyer).size() > 0) {
            //验证了用户名不为空且返回的list集合长度不为空。
            session.setAttribute("buyer", buyer);
            buyerDao.login(buyer);
            return "buyer/main";
        }
        model.addAttribute("msg", "用户名或密码错误！");//传递数据给view
        //暴露为模型数据
        return "buyer/login";
    }

    public String register(Buyerinfo buser, Model model, HttpSession session, String code) {
        if(!code.equalsIgnoreCase(session.getAttribute("code").toString())) {
            model.addAttribute("codeError", "验证码错误！");
            return "before/register";
        }
        int n = buyerDao.register(buser);
        if(n > 0) {
            return "before/login";
        }else {
            model.addAttribute("msg", "注册失败！");
            return "before/register";
        }
    }
}
